/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.utils;

import java.lang.Thread.UncaughtExceptionHandler;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

public class AppApplication extends Application implements UncaughtExceptionHandler {

	private static Context context;

	public void onCreate() {
		Thread.setDefaultUncaughtExceptionHandler(this);
	}
	
	public static void setApplicationContext(Context context) {
		AppApplication.context = context;
	}

	public static Context getAppContext() {
		return AppApplication.context;
	}

	public static void toast(int resId, int duration) {
		Toast.makeText(context, resId, duration==Toast.LENGTH_LONG ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}
	
	public static void toast(String message, int duration) {
		Toast.makeText(context, message, duration==Toast.LENGTH_LONG ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}

	public void uncaughtException(Thread thread, Throwable ex) {
		ErrorReport e = new ErrorReport();
		e.setException(ex);
		e.setEmail("");
		e.setDescription(VersionUtils.getVersion() + ". Uncaught exception " + ex.getClass().toString());
		FogbugzErrorReporter.report(e);
	}
	
}